<template>
  <el-dialog
    title="选择相对岗位"
    :visible.sync="dialogVisible"
    append-to-body
    width="800px">
    <el-form :model="form" ref="form" label-width="80px" size="mini">
      <el-row>
        <el-col :span="8">
          <el-form-item label="相对组织" prop="type">
            <el-select v-model="form.type" placeholder="相对组织">
              <el-option
                      v-for="(item,index) in Const.RelativeType"
                      :key="index"
                      :label="item.label"
                      :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8" v-if="form.type==2">
          <el-form-item label="字段Id" prop="type">
            <el-input v-model="form.field" placeholder="字段ID">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="相对关系" prop="type">
            <el-select v-model="form.relation" placeholder="相对关系">
              <el-option
                      v-for="(item,index) in Const.RelationType"
                      :key="index"
                      :label="item.label"
                      :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="岗位" prop="type">
            <el-select v-model="form.postId" placeholder="相对关系" filterable :filter-method="filterMethod">
              <el-option
                v-for="item in postSelectOptions"
                :key="item.postId"
                :label="item.postName"
                :value="item.postId">
                <span style="float: left">{{ item.postName }}</span>
                <span style="float: right; color: #8492a6; font-size: 13px">{{ item.postId }}</span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="confirm">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
  import Const from './Const'
    export default {
        props: {
            value: {
                type: [Object],
                default() {
                    return []
                }
            },
        },
        inject: ['postList'],
        components: {},
        data() {
            return {
                form: {
                  type:'',
                  relation:'',
                  post:'',
                  field:'',
                },
                dialogVisible: false,
              Const: Const,
                loading: false,
                postOptions: [],
                postSelectOptions: [],
            }
        },
        computed: {},
        watch: {},
        methods: {
            /** 查询部门下拉树结构 */
            getTreeselect() {
                this.postOptions = this.postList()
                this.postSelectOptions =   this.postOptions
            },
          filterMethod(query){
            if (query !== '') {
              this.postSelectOptions = this.postOptions.filter(item => {
                return (item.postId+'').toLowerCase()
                        .indexOf(query.toLowerCase()) > -1||item.postName.toLowerCase()
                        .indexOf(query.toLowerCase()) > -1
              })
            } else {
              this.postSelectOptions = this.postOptions
            }

          },
            confirm() {
              const post = this.postOptions.find(x=>x.postId=== this.form.postId)
              if(this.form.type!==2){
                this.form.field = ''
              }
              this.$emit('input',{
                  ...this.form,
                postName:post.postName
                })
                this.dialogVisible = false
            },
            show() {
              this.form = {
                ...this.value
              }
                this.dialogVisible = true
                this.getTreeselect()
            }
        },
        created() {

        },
        mounted() {

        },
    }
</script>
